#include "railme.h"
#include "ui_railme.h"

Railme::Railme(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::Railme)
{
    ui->setupUi(this);
    //showFullScreen();
}

Railme::~Railme()
{
    delete ui;
}

void Railme::on_actionNew_Game_triggered()
{
    cw = new ClientWindow(this);
    connect(cw,SIGNAL(connectionStateChanged(QAbstractSocket::SocketState)),this,SLOT(onConnectionStateChanged(QAbstractSocket::SocketState)));
   // qDebug() << "connected this shit :<";
    connect(cw, SIGNAL(newPlayerConnected(ConnectionPlayer*)),this, SLOT(onNewPlayerConnected(ConnectionPlayer*)));
    cw->show();
}

void Railme::on_actionHost_Game_triggered()
{
    sw = new ServerWindow(this);
    sw->show();
}

void Railme::onNewPlayerConnected(ConnectionPlayer* connPlayer)
{
	//qDebug() << "adding new remote player start";
	Player* player = new Player(0,0);
	//connPlayer->setPlayer(player);
	player->setIsRemote(true);
	//qDebug() << "is remote set to true";
	player->setConnectionPlayer(connPlayer);
	//qDebug() << "connection player assigned";
	ui->view->getMap()->addPlayer(player, true);
	//qDebug() << "player added to map";
	player->setIsAlive(false);
	//qDebug() << "player set dead";
}

void Railme::onConnectionStateChanged(QAbstractSocket::SocketState state) {
	Player* player = NULL;
	switch (state) {
		case QAbstractSocket::UnconnectedState:
			ui->view->getMap()->removePlayers();
			break;
		case QAbstractSocket::HostLookupState:

			break;
		case QAbstractSocket::ConnectingState:

			break;
		case QAbstractSocket::ConnectedState:
			player = new Player(0,0);
			player->setClientConnection(cw->getClient());
			//cw->getClient()->setPlayer(player);
			ui->view->getMap()->addPlayer(player, false, ui->view->getMouse());
			player->setIsAlive(false);

			/*player2 = new Player(300,-300);

			player2->setIsRemote(true);
			player2->setConnectionPlayer(new ConnectionPlayer());
			ui->view->getMap()->addPlayer(player2, true);
			player2->setIsAlive(true);*/
			break;
		case QAbstractSocket::BoundState:

			break;
		case QAbstractSocket::ClosingState:

			break;
		case QAbstractSocket::ListeningState:

			break;
	}
}
